﻿using System;
using System.Collections.Generic;
using System.IO;
using GameObjects;
using WebReferenceManager.ServiceReference1;
using WindowSystem;

namespace WindowsGame
{
    public sealed class NewGame : Window
    {
        private ComboBox MapName;
        private TextBox GameName, TurnTimeLimit;
        public int? GameId;

        private WebReferenceManager.ServerConnection serverConnection;
        private Game1 game;
        private GUIManager gui;

        public NewGame(Game1 game, GUIManager gui, WebReferenceManager.ServerConnection serverConnection) : base(game, gui)
        {
            this.game = game;
            this.gui = gui;
            this.serverConnection = serverConnection;

            Width = 300;
            Height = 200;
            const short controlPadding = 30;

            TitleText = "New Game";

            gui.Add(this);

            Label server;
            Add(server = new Label(game, gui) { Text = "Server: " + serverConnection.Address, X = 10, Y = 15, Height = 20, });
            server.Width = Width - server.X - controlPadding;
            
            Add(new Label(game, gui) { Text = "Map", X = 10, Y = 45, Width = 50, Height = 20, });
            MapName = new ComboBox(game, gui) { X = 60, Y = 40, };
            MapName.Width = Width - MapName.X - controlPadding;

            try
            {
                var maps = serverConnection.WebService.GetMaps();
                foreach (var mapDto in maps)
                    MapName.AddEntry(mapDto.Id, mapDto.Name);
            }
            catch (Exception ex)
            {
                new MessageBox(game, gui, "Could not retreive map list from server. " + ex.Message, "Server error",MessageBoxButtons.OK, MessageBoxType.Error).Show(true);
                CloseWindow();
            }

            Add(MapName);

            Add(new Label(game, gui) { Text = "Name", X = 10, Y = MapName.Y + 35, Width = 60, Height = 20, });
            GameName = new TextBox(game, gui) { X = 60, Y = MapName.Y + 30, };
            GameName.Width = Width - GameName.X - controlPadding;
            Add(GameName);

            Add(new Label(game, gui) { Text = "Turn Time Limit", X = 10, Y = GameName.Y + 35, Width = 120, Height = 20, });
            Add(TurnTimeLimit = new TextBox(game, gui) { Text = "00:05:00", X = 120, Y = GameName.Y + 30, });
            TurnTimeLimit.Width = Width - TurnTimeLimit.X - controlPadding;

            var btnOk = new TextButton(game, gui) { Text = "Ok", X = 5, Y = Height - 65, };
            btnOk.Click += (btnOk_Click);
            Add(btnOk);

            CenterWindow();
        }

        void btnOk_Click(UIComponent sender)
        {
            if (string.IsNullOrEmpty(MapName.SelectedText))
            {
                var mb = new MessageBox(game, gui, "Please select a map", "No map selected", MessageBoxButtons.OK, MessageBoxType.Error);
                mb.Show(true);
                return;
            }
            if (string.IsNullOrEmpty(GameName.Text))
            {
                var mb = new MessageBox(game, gui, "Please enter a name for the game", "No name entered", MessageBoxButtons.OK, MessageBoxType.Error);
                mb.Show(true);
                return;
            }

            TimeSpan timeLimit;
            try
            {
                timeLimit = GameState.ParseTurnTimeLimit(TurnTimeLimit.Text);
            }
            catch (Exception ex)
            {
                (new MessageBox(game, gui, ex.Message, "Time limit error", MessageBoxButtons.OK, MessageBoxType.Error)).Show(true);
                return;
            }
            
            GameId = serverConnection.WebService.CreateGame(MapName.SelectedItemKey, GameName.Text, 15, timeLimit);

            CloseWindow();
        }

    }
}